import 'package:flutter/material.dart';
import 'package:dustbin_sort/pages/InfoPage.dart';
import 'package:dustbin_sort/pages/MyPage.dart';
import 'package:dustbin_sort/pages/PhotoPage.dart';
import 'package:dustbin_sort/pages/home_page.dart';
import 'package:dustbin_sort/pages/search_page.dart';
import 'package:flutter_splash_screen/flutter_splash_screen.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:package_info/package_info.dart';

class TabNavigator extends StatefulWidget
{
  @override
  State<StatefulWidget> createState()=>_TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator>{
	final _defaultColor = Colors.grey;
	final _activeColor = Colors.blue;
	var _controller = PageController(initialPage: 0);
	int _currentIndex = 0;
	DateTime _lastPressedAt; //上次点击时间

	@override
	void initState() {
		hideScreen();
		getPackageInfo();
		super.initState();

	}


	@override
	void dispose() {
		_controller.dispose();
		super.dispose();
	}

	//隐藏启动屏
	Future<void> hideScreen() async {
		Future.delayed(Duration(milliseconds: 2000), () {
			FlutterSplashScreen.hide();
		});
	}

	//退出app
	Future<bool> exitApp() {
		if (_lastPressedAt == null ||
				DateTime.now().difference(_lastPressedAt) > Duration(seconds: 2)) {
			Fluttertoast.showToast(
					msg: "再按一次退出应用",
					backgroundColor: Colors.grey,
					toastLength: Toast.LENGTH_SHORT,
					fontSize: 14);
			//两次点击间隔超过2秒则重新计时
			_lastPressedAt = DateTime.now();
			return Future.value(false);
		}
		return Future.value(true);

	}

	//获取packageInfo
	void getPackageInfo() async {
		PackageInfo packageInfo = await PackageInfo.fromPlatform();
		String appName = packageInfo.appName;
		String packageName = packageInfo.packageName;
		String version = packageInfo.version;
		String buildNumber = packageInfo.buildNumber;
		print(
				'appName:$appName,packageName:$packageName,version:$version,buildNumber:$buildNumber}');
	}

	@override
	Widget build(BuildContext context) {
		return Scaffold(
			body: WillPopScope(
					child: PageView(
						physics: NeverScrollableScrollPhysics(),
						controller: _controller,
						children: <Widget>[
							HomePage(),
							SearchPage(),
							PhotoPage(),
							InfoPage(),
							MyPage(),
						],
					),
					onWillPop: exitApp),
			bottomNavigationBar: BottomNavigationBar(
					selectedFontSize: 10,
					unselectedFontSize: 10,
					currentIndex: _currentIndex,
					onTap: (index) {
						_controller.jumpToPage(index);
						setState(() {
							_currentIndex = index;
						});
					},
					type: BottomNavigationBarType.fixed,
					items: [
						_bottomItem(Icons.home, '首页', 0),
						_bottomItem(Icons.search, '搜索', 1),
						_bottomItem(Icons.camera_alt, '拍照', 2),
						_bottomItem(Icons.insert_chart, '科普', 3),
						_bottomItem(Icons.account_circle, '我的', 4),
					]),
		);
	}

	//底部导航item
	BottomNavigationBarItem _bottomItem(IconData icon, String title, int index) {
		return BottomNavigationBarItem(
				icon: Icon(
					icon,
					color: _defaultColor,
				),
				activeIcon: Icon(
					icon,
					color: _activeColor,
				),
				title: Text(
					title,
					style: TextStyle(
							color: _currentIndex != index ? _defaultColor : _activeColor
					),
				)
		);
	}
}